
# Copyright (c) WanSheng Intelligent Corp. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.


cmake_minimum_required(VERSION 3.0.0)
message("s7 PLC plugin...")
project(s7)


if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/libnodave-src)

# Download and unpack googletest at configure time
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt.in libnodave-download/CMakeLists.txt)

message(STATUS "s7: CMAKE_BINARY_DIR is ${CMAKE_BINARY_DIR}")
message(STATUS "s7: CMAKE_CURRENT_BINARY_DIR is ${CMAKE_CURRENT_BINARY_DIR}")

execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/libnodave-download )
if(result)
  message(FATAL_ERROR "CMake step for libnodave failed: ${result}")
endif()

execute_process(COMMAND ${CMAKE_COMMAND} --build .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/libnodave-download )
if(result)
  message(FATAL_ERROR "Build step for libnodave failed: ${result}")
endif()

endif()


add_definitions(-Wall  -DLINUX -DDAVE_LITTLE_ENDIAN)
# -Winline  -fPIC

#set paths
set(LIBNODAVE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libnodave-src)
set(REPO_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../..)

include(${WA_SDK_DIR}/wa-sdk.cmake)
include (${WA_SDK_DIR}/rules.cmake)

include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${LIBNODAVE_DIR}
)

set(s7_plugin_sources
    ${SSG_UTILS_SOURCE}
    ${EX_AMS_SOURCE}
    ${EX_COAP_SOURCE}
    ${CMAKE_CURRENT_SOURCE_DIR}/s7-module.c
    ${CMAKE_CURRENT_SOURCE_DIR}/resources.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/routines.cpp
    ${LIBNODAVE_DIR}/nodave.c
    ${LIBNODAVE_DIR}/setport.c
    ${LIBNODAVE_DIR}/openSocket.c
    ${LIBNODAVE_DIR}/nodave.c
    ${LIBNODAVE_DIR}/nodave.c
)

set(s7_plugin_headers
    ${CMAKE_CURRENT_SOURCE_DIR}/s7-plugin.h
)

#this builds the hello_world dynamic library
add_library(s7_plugin MODULE ${s7_plugin_sources} ${s7_plugin_headers})
linkWA_SDK(s7_plugin)

